package org.bw.bookstore.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.bw.bookstore.book.config.OssUtile;
import org.bw.bookstore.book.mapper.*;
import org.bw.bookstore.book.service.BookService;
import org.bw.bookstore.common.entity.*;
import org.bw.bookstore.common.utils.ConstantSet;
import org.bw.bookstore.common.utils.ResponseResult;
import org.bw.bookstore.common.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
*@program: wisdom_bookstore
*@description: 书籍接口实现
*@author: 孙冬雪
*@create: 2020-10-25 14:33
*/
@Service
@Transactional
@Slf4j
public class BookServiceImpl extends ServiceImpl<BookSingleMapper, TbBookSingle> implements BookService {

    @Autowired
    private BookDetailedMapper bookDetailedMapper;

    @Autowired
    private BookTypeMapper bookTypeMapper;

    @Autowired
    private BookSingleMapper bookSingleMapper;

    @Autowired
    private RlBookAndTypeMapper rlBookAndTypeMapper;

    @Autowired
    private BookImgMapper bookImgMapper;

    public BaseEntity init(BaseEntity baseEntity){
        baseEntity.setCode(StringTools.getRandomString(32));
        baseEntity.setDeleted(ConstantSet.NOT);
        baseEntity.setCreateTime(new Date());
        return baseEntity;
    }

    /**
     * @Description 用户页面浏览书籍
     * TODO 孙冬雪--> findAllBookDetailed 查看所有图书
     * @Param []
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/25 10:09
     **/
    @Override
    public ResponseResult findAllBookDetailed() {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //查询所有未删除已上架的书籍
        List<TbBookDetailed> findBookDetailedList = bookDetailedMapper.selectList(
                new QueryWrapper<TbBookDetailed>()
                        .lambda()
                        .eq(TbBookDetailed::getDeleted, ConstantSet.NOT)
        );
        if (findBookDetailedList == null){
            return result.setFail("当前并没有书籍");
        }

        //返回所有书籍
        resultMap.put("bookDetailedList", findBookDetailedList);
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description 图书上货时添加
     * TODO 孙冬雪--> addBookDetailed 添加图书信息
     * @Param [bookDetailed]
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/25 10:09
     **/
    @Override
    public ResponseResult addBookDetailed(TbBookDetailed bookDetailed) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //查找是否存在此书籍详情
        TbBookDetailed findBookDetailed = bookDetailedMapper.selectOne(
                new QueryWrapper<TbBookDetailed>()
                        .lambda()
                        .eq(TbBookDetailed::getBookName, bookDetailed.getBookName())
                        .eq(TbBookDetailed::getDeleted, ConstantSet.NOT)
        );
        if (findBookDetailed != null){
            if (findBookDetailed.getOnTheShelf().equals(ConstantSet.NOT)){
                return result.setFail("该书籍已下架，您可以先上架");
            }
            return result.setFail("该书籍已存在，不能再添加");
        }

        //添加图书详情
        bookDetailed = (TbBookDetailed)this.init(bookDetailed);
        bookDetailed.setBookInventoryLevel(0);
        bookDetailed.setOnTheShelf(ConstantSet.YES);
        bookDetailedMapper.insert(bookDetailed);

        //批量添加对应图书和图书的类型
        if (bookDetailed.getBookTypeCodes() != null && !bookDetailed.getBookTypeCodes().equals("")){
            this.addRlBookAndTypeBash(bookDetailed.getBookTypeCodes(), bookDetailed.getCode());
        }

        //批量添加图书图片
        if (bookDetailed.getBookImgCodes() != null && !bookDetailed.getBookImgCodes().equals("")) {
            this.addBookImgBash(bookDetailed.getBookImgCodes(), bookDetailed.getCode());
        }

        //添加图书单品
        if (bookDetailed.getTbBookSingleList() != null && bookDetailed.getTbBookSingleList().size() > 0){
            List<TbBookSingle> bookSingleList = bookDetailed.getTbBookSingleList();
            for (TbBookSingle bookSingle : bookSingleList){
                bookSingle.setBookDetailedCode(bookDetailed.getCode());
                bookSingle.setOnSellStatus(ConstantSet.NOT);
                this.addBookSingle(bookSingle);
            }
        }
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description
     * TODO 孙冬雪--> delBookDetailed 下架图书
     * @Param [bookDetailedId]
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/25 10:09
     **/
    @Override
    public ResponseResult delBookDetailed(Integer bookDetailedId, Integer detailedId) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        System.err.println(detailedId+"----"+bookDetailedId);
        //查询该书籍详情是否存在
        TbBookDetailed findBookDetailed = bookDetailedMapper.selectOne(
                new QueryWrapper<TbBookDetailed>()
                        .lambda()
                        .eq(TbBookDetailed::getId, bookDetailedId)
                        .eq(TbBookDetailed::getDeleted, ConstantSet.NOT)
                        .last("limit 1")
        );
        if (findBookDetailed == null){
            return result.setFail("书籍不存在，下架无效");
        }
        if (findBookDetailed.getOnTheShelf().equals(detailedId)){
            return result.setFail("操作无效");
        }

        //下架书籍详情
        findBookDetailed.setOnTheShelf(detailedId);
        findBookDetailed.setUpdateTime(new Date());
        bookDetailedMapper.updateById(findBookDetailed);
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description 
     * TODO 孙冬雪--> updateBookDetailed 修改图书详细信息
     * @Param [bookDetailed]
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/25 10:09
     **/
    @Override
    public ResponseResult updateBookDetailed(TbBookDetailed bookDetailed) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //查找是否存在此书籍详情
        TbBookDetailed findBookDetailed = bookDetailedMapper.selectOne(
                new QueryWrapper<TbBookDetailed>()
                        .lambda()
                        .eq(TbBookDetailed::getDeleted, ConstantSet.NOT)
                        .eq(TbBookDetailed::getId, bookDetailed.getId())
        );
        if (findBookDetailed == null){
            return result.setFail("该书籍不存在");
        }

        //判断是否已经存在新修改的书籍
        TbBookDetailed tbBookDetailed = bookDetailedMapper.selectOne(
                new QueryWrapper<TbBookDetailed>()
                        .lambda()
                        .eq(TbBookDetailed::getDeleted, ConstantSet.NOT)
                        .eq(TbBookDetailed::getBookName, bookDetailed.getBookName())
                        .ne(TbBookDetailed::getId, bookDetailed.getId())
                        .last("limit 1")
        );
        if (tbBookDetailed != null){
            return result.setFail("该书籍已存在，请重新修改");
        }

        //修改图书详情
        bookDetailed.setUpdateTime(new Date());
        bookDetailedMapper.updateById(bookDetailed);

        //删除原来对应的图书类型
        rlBookAndTypeMapper.delete(
                new QueryWrapper<RlBookAndType>()
                        .lambda()
                        .eq(RlBookAndType::getBookDetailedCode, bookDetailed.getCode())
        );
        //批量添加对应图书和图书的类型
        if (bookDetailed.getBookTypeCodes() != null && !bookDetailed.getBookTypeCodes().equals("")) {
            this.addRlBookAndTypeBash(bookDetailed.getBookTypeCodes(), bookDetailed.getCode());
        }

        //删除原来的图书照片
        bookImgMapper.delete(
                new QueryWrapper<TbBookImg>()
                        .lambda()
                        .eq(TbBookImg::getBookDetailedCode, bookDetailed.getCode())
        );
        //批量添加图书图片
        if (bookDetailed.getBookImgCodes() != null && !bookDetailed.getBookImgCodes().equals("")) {
            this.addBookImgBash(bookDetailed.getBookImgCodes(), bookDetailed.getCode());
        }

        //删除原来的书籍单品
        /*bookSingleMapper.delete(
                new QueryWrapper<TbBookSingle>()
                        .lambda()
                        .eq(TbBookSingle::getBookDetailedCode, bookDetailed.getCode())
        );*/

        //查询原来的书籍单品
        List<TbBookSingle> bookSingleList = bookSingleMapper.selectList(
                new QueryWrapper<TbBookSingle>()
                        .lambda()
                        .eq(TbBookSingle::getBookDetailedCode, bookDetailed.getCode())
                        .eq(TbBookSingle::getDeleted, ConstantSet.NOT)
        );

        //添加书籍单品
        List<TbBookSingle> tbBookSingleList = bookDetailed.getTbBookSingleList();
        for (TbBookSingle bookSingle : tbBookSingleList) {
            if(bookSingle.getCode() == null) {
                bookSingle.setBookDetailedCode(bookDetailed.getCode());
                bookSingle = (TbBookSingle) this.init(bookSingle);
                this.addBookSingle(bookSingle);
            }
        }


        /*this.saveBatch(tbBookSingleList);*/
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description 用户过扫描机
     * TODO 孙冬雪--> findDetailedByRFID 根据RFID返回未付款书籍详情
     * @Param [rfids]
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/25 10:11
     **/
    @Override
    public ResponseResult findDetailedByRFID(String rfids) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //判断并进行rfids拆分
        if(rfids == null || rfids.equals("")){
            return result.setFail("没有rfids");
        }
        String[] split = rfids.split(",");

        //查找所有rfid的图书详情
        List<TbBookDetailed> bookDetailedList = bookDetailedMapper.findByRFID(split);
        //查找所有的rfid的单品记录
        List<TbBookSingle> bookSingleList = bookDetailedMapper.findSingleByRFID(split);

        //循环书籍详情
        for (TbBookDetailed bookDetailed : bookDetailedList){
            List<TbBookSingle> tbBookSingleList = new ArrayList<>();
            //循环单品书籍
            for (TbBookSingle bookSingle : bookSingleList){
                //如果已售卖就跳过
                if (bookSingle.getOnSellStatus().equals(ConstantSet.YES)){
                    continue;
                }
                //如果单品书籍和图书详情互相对应 就存到集合中
                if (bookDetailed.getCode().equals(bookSingle.getBookDetailedCode())){
                    tbBookSingleList.add(bookSingle);
                }
            }
            //添加到图书详情里
            bookDetailed.setTbBookSingleList(tbBookSingleList);
        }

        //返回结果
        resultMap.put("bookDetailedList", bookDetailedList);
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description
     * TODO 孙冬雪--> findDetailedById 根据ID查找对应书籍详情
     * @Param [id]
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/25 15:46
     **/
    @Override
    public ResponseResult findDetailedById(Integer id) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //查找是否存在此书籍详情
        TbBookDetailed findBookDetailed = bookDetailedMapper.selectOne(
                new QueryWrapper<TbBookDetailed>()
                        .lambda()
                        .eq(TbBookDetailed::getId, id)
                        .eq(TbBookDetailed::getDeleted, ConstantSet.NOT)
                        .last("limit 1")
        );
        if (findBookDetailed == null){
            return result.setFail("该书籍不存在");
        }

        //查询对应的单品书籍
        List<TbBookSingle> findBookSingleList = bookSingleMapper.selectList(
                new QueryWrapper<TbBookSingle>()
                        .lambda()
                        .eq(TbBookSingle::getDeleted, ConstantSet.NOT)
                        .eq(TbBookSingle::getBookDetailedCode, findBookDetailed.getCode())
        );
        findBookDetailed.setTbBookSingleList(findBookSingleList);

        //查询对应的书籍插图
        List<TbBookImg> findBookImgList = bookImgMapper.selectList(
                new QueryWrapper<TbBookImg>()
                        .lambda()
                        .eq(TbBookImg::getDeleted, ConstantSet.NOT)
                        .eq(TbBookImg::getBookDetailedCode, findBookDetailed.getCode())
        );
        findBookDetailed.setBookImgList(findBookImgList);

        //查询对应的书籍类型
        List<TbBookType> findBookTypeList = bookTypeMapper.findTypeByDetailed(findBookDetailed.getCode());
        findBookDetailed.setTbBookTypeList(findBookTypeList);

        resultMap.put("bookDetailed", findBookDetailed);
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description
     * TODO 孙冬雪--> addBookImgBash 批量添加图书插图
     * @Param []
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/24 16:37
     *
     * @param bookImgUrls
     * @param bookDetailedCode*/
    @Override
    public ResponseResult addBookImgBash(String bookImgUrls, String bookDetailedCode) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //判断书籍详情code是否正确
        TbBookDetailed bookDetailed = bookDetailedMapper.selectOne(
                new QueryWrapper<TbBookDetailed>()
                        .lambda()
                        .eq(TbBookDetailed::getDeleted, ConstantSet.NOT)
                        .eq(TbBookDetailed::getOnTheShelf, ConstantSet.YES)
                        .eq(TbBookDetailed::getCode, bookDetailedCode)
                        .last("limit 1")
        );
        if (bookDetailed == null){
            return result.setFail("传入书籍错误错误");
        }

        //依次添加书籍图片
        String[] split = bookImgUrls.split(",");
        for (String bookImgUrl : split){
            TbBookImg bookImg = new TbBookImg(bookImgUrl, bookDetailedCode, null);
            /*bookImg.setBookDetailedCode(bookDetailedCode);
            bookImg.setBookImgUrl(bookImgUrl);*/
            bookImg = (TbBookImg)this.init(bookImg);
            bookImgMapper.insert(bookImg);
        }
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description
     * TODO 孙冬雪--> findBookImgByBook 根据图书查找对应插图
     * @Param []
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/24 16:37
     *
     * @param bookDetailedCode*/
    @Override
    public ResponseResult findBookImgByBook(String bookDetailedCode) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //判断书籍详情code是否正确
        TbBookDetailed findBookDetailed = bookDetailedMapper.selectOne(
                new QueryWrapper<TbBookDetailed>()
                        .lambda()
                        .eq(TbBookDetailed::getDeleted, ConstantSet.NOT)
                        .eq(TbBookDetailed::getOnTheShelf, ConstantSet.YES)
                        .eq(TbBookDetailed::getCode, bookDetailedCode)
                        .last("limit 1")
        );
        if (findBookDetailed == null){
            return result.setFail("传入书籍错误错误");
        }

        //根据书籍code查找书籍插图
        List<TbBookImg> tbBookImgList = bookImgMapper.selectList(
                new QueryWrapper<TbBookImg>()
                        .lambda()
                        .ne(TbBookImg::getDeleted, ConstantSet.YES)
                        .eq(TbBookImg::getBookDetailedCode, bookDetailedCode)
        );
        if (tbBookImgList == null){
            return result.setFail("该图书没有插图");
        }

        //返回书籍插图
        resultMap.put("bookImgList", tbBookImgList);
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description
     * TODO 孙冬雪--> sellBookSingle 售卖书籍单品
     * @Param [id]
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/24 8:40
     **/
    @Override
    public ResponseResult sellBookSingle(Integer bookSingleId) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //查询该书籍单品是否存在
        TbBookSingle findBookSingle = bookSingleMapper.selectOne(
                new QueryWrapper<TbBookSingle>()
                        .lambda()
                        .eq(TbBookSingle::getId, bookSingleId)
                        .eq(TbBookSingle::getDeleted, ConstantSet.NOT)
                        .ne(TbBookSingle::getOnSellStatus, ConstantSet.YES)
                        .last("limit 1")
        );

        if (findBookSingle == null){
            return result.setFail("书籍不存在");
        }

        //计算书籍详情库存
        TbBookDetailed findBookDetailed = bookDetailedMapper.selectOne(
                new QueryWrapper<TbBookDetailed>()
                        .lambda()
                        .eq(TbBookDetailed::getDeleted, ConstantSet.NOT)
                        .eq(TbBookDetailed::getOnTheShelf, ConstantSet.YES)
                        .eq(TbBookDetailed::getCode, findBookSingle.getBookDetailedCode())
        );
        if (findBookDetailed.getBookInventoryLevel() <= 0){
            return result.setFail("没有库存了，请过两天再来哦");
        }

        //书籍单品售出
        findBookDetailed.setBookInventoryLevel(findBookDetailed.getBookInventoryLevel()-1);
        findBookDetailed.setUpdateTime(new Date());
        bookDetailedMapper.updateById(findBookDetailed);
        findBookSingle.setOnSellStatus(ConstantSet.YES);
        findBookSingle.setUpdateTime(new Date());
        bookSingleMapper.updateById(findBookSingle);

        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description
     * TODO 孙冬雪--> addBookType 添加图书类型
     * @Param [bookType]
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/23 10:21
     **/
    @Override
    public ResponseResult addBookType(TbBookType bookType) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //判断传入书籍类型名称是否正确
        if (bookType == null || bookType.getBookTypeName() == null || bookType.getBookTypeName().equals("")){
            return result.setFail("图书类型不能为空");
        }

        //判断该书籍类型是否存在
        TbBookType findBookType = bookTypeMapper.selectOne(
                new QueryWrapper<TbBookType>()
                        .lambda()
                        .eq(TbBookType::getBookTypeName, bookType.getBookTypeName())
                        .eq(TbBookType::getDeleted, ConstantSet.NOT)
                        .last("limit 1")
        );
        if (findBookType != null){
            return result.setFail("该书籍类型已存在");
        }

        //添加书籍类型
        bookType = (TbBookType)this.init(bookType);
        bookTypeMapper.insert(bookType);
        if (bookType.getId() == null){
            return result.setFail("书籍类型添加失败");
        }
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description
     * TODO 孙冬雪--> delBookType 删除图书类型
     * @Param [bookTypeId]
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/23 10:21
     **/
    @Override
    public ResponseResult delBookType(Integer bookTypeId) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //判断id是否为空
        if (bookTypeId == null || bookTypeId.equals("") || bookTypeId == 0){
            return result.setFail("传入非法参数");
        }
        //判断该类型是否存在或删除
        TbBookType findBookType = bookTypeMapper.selectOne(
                new QueryWrapper<TbBookType>()
                        .lambda()
                        .eq(TbBookType::getId, bookTypeId)
                        .eq(TbBookType::getDeleted, ConstantSet.NOT)
                        .last("limit 1")
        );
        if (findBookType == null){
            return result.setFail("该书籍类型不存在,删除无效");
        }

        //设置书籍类型状态为删除
        findBookType.setDeleted(ConstantSet.YES);
        findBookType.setUpdateTime(new Date());
        bookTypeMapper.updateById(findBookType);
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description
     * TODO 孙冬雪--> updateBookType 修改图书类型
     * @Param [bookType]
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/23 10:21
     **/
    @Override
    public ResponseResult updateBookType(TbBookType bookType) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //判断该ID是否存在或删除(书籍不存在或已删除不能修改)
        TbBookType tbBookType = bookTypeMapper.selectOne(
                new QueryWrapper<TbBookType>()
                        .lambda()
                        .eq(TbBookType::getId, bookType.getId())
                        .ne(TbBookType::getDeleted, ConstantSet.YES)
                        .last("limit 1")
        );

        if (tbBookType == null){
            return result.setFail("该书籍不存在或已删除，不能进行修改");
        }

        //判断该书籍类型名称是否存在
        TbBookType findBookType = bookTypeMapper.selectOne(
                new QueryWrapper<TbBookType>()
                        .lambda()
                        .eq(TbBookType::getBookTypeName, bookType.getBookTypeName())
                        .ne(TbBookType::getDeleted, ConstantSet.YES)
                        .last("limit 1")
        );
        if (findBookType != null){
            return result.setFail("该书籍类型已存在,请重新修改");
        }

        //修改书籍
        bookType.setUpdateTime(new Date());
        bookTypeMapper.updateById(bookType);
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description
     * TODO 孙冬雪--> findAllBookType 查询所有图书类型
     * @Param []
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/23 10:21
     **/
    @Override
    public ResponseResult findAllBookType() {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //查找所有存在的图书类型
        List<TbBookType> tbBookTypeList = bookTypeMapper.selectList(
                new QueryWrapper<TbBookType>()
                        .lambda()
                        .eq(TbBookType::getDeleted, ConstantSet.NOT)
        );
        if (tbBookTypeList == null || tbBookTypeList.size() <= 0){
            return result.setFail("当前并没有书籍类型");
        }

        //查找到并返回
        resultMap.put("bookTypeList", tbBookTypeList);
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description
     * TODO 孙冬雪--> findBookTypeById 根据图书类型ID查找图书类型
     * @Param [bookTypeId]
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/25 14:33
     **/
    @Override
    public ResponseResult findBookTypeById(Integer bookTypeId) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //查找是否存在此书籍类型
        TbBookType tbBookType = bookTypeMapper.selectOne(
                new QueryWrapper<TbBookType>()
                        .lambda()
                        .eq(TbBookType::getId, bookTypeId)
                        .eq(TbBookType::getDeleted, ConstantSet.NOT)
                        .last("limit 1")
        );
        if (tbBookType == null){
            return result.setFail("该书籍类型不存在");
        }

        resultMap.put("bookType", tbBookType);
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description 
     * TODO 孙冬雪--> uploadImg 上传图片
     * @Param [fileBytes, fileExt] 
     * @Return java.lang.String
     * @Author 孙冬雪
     * @Date 2020/10/25 15:51
     **/
    @Override
    public ResponseResult uploadImg(MultipartFile file) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //上传图片并返回
        String url = OssUtile.UploadAliyun(file, file.getOriginalFilename());
        System.err.println(url);
        resultMap.put("imgUrl", url);
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description 
     * TODO 孙冬雪--> addBookSingle 添加书籍单品
     * @Param [bookSingle] 
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/26 12:00
     **/
    @Override
    public ResponseResult addBookSingle(TbBookSingle bookSingle) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //判断有没有这个书籍详情
        TbBookDetailed findBookDetailed = bookDetailedMapper.selectOne(
                new QueryWrapper<TbBookDetailed>()
                        .lambda()
                        .eq(TbBookDetailed::getDeleted, ConstantSet.NOT)
                        .eq(TbBookDetailed::getOnTheShelf, ConstantSet.YES)
                        .eq(TbBookDetailed::getCode, bookSingle.getBookDetailedCode())
                        .last("limit 1")
        );
        if (findBookDetailed == null){
            return result.setFail("没有该书籍，请先添加书籍");
        }

        //判断是否有这个单品
        TbBookSingle findBookSingle = bookSingleMapper.selectOne(
                new QueryWrapper<TbBookSingle>()
                        .lambda()
                        .eq(TbBookSingle::getDeleted, ConstantSet.NOT)
                        .ne(TbBookSingle::getOnSellStatus, ConstantSet.YES)
                        .eq(TbBookSingle::getRfid, bookSingle.getRfid())
                        .last("limit 1")
        );
        if (findBookSingle != null){
            return result.setFail("RFID错误");
        }

        //添加图书单品
        bookSingle.setOnSellStatus(ConstantSet.NOT);
        bookSingle = (TbBookSingle)this.init(bookSingle);
        bookSingleMapper.insert(bookSingle);
        findBookDetailed.setBookInventoryLevel(findBookDetailed.getBookInventoryLevel()+1);
        findBookDetailed.setUpdateTime(new Date());
        bookDetailedMapper.updateById(findBookDetailed);
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description
     * TODO 孙冬雪--> delBookSingle 删除书籍单品
     * @Param [bookSingleId]
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/27 9:22
     **/
    @Override
    public ResponseResult delBookSingle(Integer bookSingleId) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //判断是否有这个单品
        TbBookSingle findBookSingle = bookSingleMapper.selectOne(
                new QueryWrapper<TbBookSingle>()
                        .lambda()
                        .eq(TbBookSingle::getDeleted, ConstantSet.NOT)
                        .ne(TbBookSingle::getOnSellStatus, ConstantSet.YES)
                        .eq(TbBookSingle::getId, bookSingleId)
                        .last("limit 1")
        );
        if (findBookSingle == null){
            return result.setFail("未找到该书籍单品");
        }
        if (findBookSingle.getDeleted().equals(ConstantSet.YES)){
            return result.setFail("该书籍已经删除");
        }
        if (findBookSingle.getOnSellStatus().equals(ConstantSet.NOT)){
            return result.setFail("未售卖的书籍不能删除");
        }

        //修改为删除状态
        findBookSingle.setOnSellStatus(ConstantSet.YES);
        findBookSingle.setUpdateTime(new Date());
        bookSingleMapper.updateById(findBookSingle);
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description
     * TODO 孙冬雪--> findSingleListByDetailed 根据书籍详情查找单品书籍
     * @Param [bookDetailedCode]
     * @Return com.bw.servercommon.utils.ResponseResult
     * @Author 孙冬雪
     * @Date 2020/10/27 9:22
     **/
    @Override
    public ResponseResult findSingleListByDetailed(String bookDetailedCode) {
        ResponseResult result = ResponseResult.SUCCESS();
        Map<String, Object> resultMap = new HashMap<>();

        //判断有没有这个书籍详情
        TbBookDetailed findBookDetailed = bookDetailedMapper.selectOne(
                new QueryWrapper<TbBookDetailed>()
                        .lambda()
                        .ne(TbBookDetailed::getDeleted, ConstantSet.YES)
                        .eq(TbBookDetailed::getCode, bookDetailedCode)
                        .last("limit 1")
        );
        if (findBookDetailed == null){
            return result.setFail("没有该书籍");
        }

        List<TbBookSingle> findBookSingleList = bookSingleMapper.selectList(
                new QueryWrapper<TbBookSingle>()
                        .lambda()
                        .eq(TbBookSingle::getDeleted, ConstantSet.NOT)
                        .eq(TbBookSingle::getBookDetailedCode, bookDetailedCode)
        );

        resultMap.put("bookSingleList", findBookSingleList);
        result.setResult(resultMap);
        return result;
    }

    /**
     * @Description 
     * TODO 孙冬雪--> addRlBookAndTypeBash 批量添加对应图书和图书类型
     * @Param [bookTypeCodes, bookDetailedCode] 
     * @Return void
     * @Author 孙冬雪
     * @Date 2020/10/27 10:40
     **/
    public void addRlBookAndTypeBash(String bookTypeCodes, String bookDetailedCode){
        String[] split = bookTypeCodes.split(",");

        //循环添加图书和图书类型
        for (String bookTypeCode : split){
            RlBookAndType rlBookAndType = new RlBookAndType(bookDetailedCode,bookTypeCode,null,null);
            rlBookAndType = (RlBookAndType)this.init(rlBookAndType);
            rlBookAndTypeMapper.insert(rlBookAndType);
        }
    }
}
